<!DOCTYPE html>
<html lang="en"><!-- InstanceBegin template="/Templates/MainTemplate.dwt" codeOutsideHTMLIsLocked="false" -->
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
<!-- #BeginEditable "doctitle" -->
<title>Immutability is easy</title>  
<!-- #EndEditable -->
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
    <!-- Bootstrap core CSS -->
     <link rel="stylesheet" href="css/bootstrap.min.css">   

    <!-- Custom styles for this template -->
    <link href="css/starter-template.css" rel="stylesheet">

   <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->    
  </head>
<body>
 <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.html">Jdon Framework</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
                <li class="active"><a href="index.html">Home</a></li>
               <li><a href="https://sourceforge.net/projects/jdon/files/">Download</a></li>
               <li><a href="doc.htm" >Document</a></li>
               <li><a href="quickconf.html">Qucik Start</a> </li>
               <li><a href="examples.html">Examples</a></li>
               <li ><a href="https://github.com/banq/jdonframework">Github</a></li>
               <li><a href="articles.html">Articles</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </div>
    

    <div class="container">
     <div class="starter-template">     
    
 <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">  
  <tr>     
    <td bgcolor="#FFFFFF"  valign="top" align="left">

                  
				<!-- #BeginEditable "main" -->
				<h1>Immutability is easy <br>
				  </h1>
                <p>Immutability is abstraction. It does not &quot;exist&quot; in nature,World is mutable, world is permanently changing,So it's quite natural for data structures to be mutable that they describe the state of the real or simulated object at a given moment in time. but  problems will arise  in computer world when:</p>
                <p>1.data structures are modified from concurrent threads<br>
                  2.users provide conficting changes for the same objects<br>
                  3.a user provide an invalid data and it should be rolled back</p>
                <p>how to solve these problems? solutions is :immutable or volatile  and synchronized lock.we know lock is enemy of performance, lock is blocking, and volatile has some shortcomings, The only choice is immutable.</p>
                <p>so immutable is a performance design pattern, Once it is being constructed, it will forever change,in java all fields in it is &quot;final&quot;, no setXXX method that you can change any field. when you publish a immutable object is thread safe.</p>
                <p>so if we use immutable pattern to avoid transaction lock such as database lock or java synchronized.</p>
                <p>next problem is for programmers how to know which datas is immutable?</p>
                <p>that is  Value Object of DDD(Domain-driven desgin)：</p>
                <p>&quot;An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT. VALUE OBJECTS are instantiated to represent elements of the design that we care about only for what they are, not who or which they are.&quot; [Evans 2003]<br>
                </p>
                <p>the primary difference to me between Entities and Value Objects is identity，If I have two instances to Value Objects with same internal values, they should be considered equal，If you can't make a Value Object</p>
                <p>- Immutable<br>
                  - value equality rather than reference equality</p>
                <p>Then it probably isn't a Value Object，</p>
                <p>so if a domain expert designs a object is value object, then we know the object is immutable. then software system would be pleased with it.</p>
                <p>in scala, they are happy because they can use Immutability + Actor model for concurrency programming, how to implement a immutable  value object in Java?</p>
                <p>in Java I think we can have a happy concurrency programming by Immutability + ES Model of JdonFramework, here is a immutable class from open source jivejdon:</p>
                <table width="100%"  border="0" cellpadding="1" cellspacing="0" bgcolor="#CCCCCC">
                  <tr bgcolor="#CCCCCC">
                    <td><P>public class ForumState {<br>
                      private final AtomicLong threadCount;<br>
                      <br>
                      private final AtomicLong messageCount;</P>
                      <p> private final ForumMessage lastPost;</p>
                      <p> private final Forum forum;</p>
                      <p> private final SubscribedState subscribedState;</p>
                      <p> public ForumState(Forum forum, ForumMessage lastPost, long messageCount, long threadCount) {<br>
                        super();<br>
                        this.forum = forum;<br>
                        this.lastPost = lastPost;<br>
                        this.messageCount = new AtomicLong(messageCount);<br>
                        this.threadCount = new AtomicLong(threadCount);<br>
                        this.subscribedState = new SubscribedState(new ForumSubscribed(forum));<br>
                        }<br>
                        ...<br>
                        }<br>
                      </p></td>
                  </tr>
                </table>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>I use &quot;final&quot; for this value object:ForumState, so these fields in VO never be modified seperately,<br>
                  if you want to change one of them, you must replcae the whole VO, replacing a object is a atomc operating,<br>
                  so no need any locks.</p>
                <p>according DDD, we need a Factory to manage  immutable objects's lifecycle, so I create a Factory for creating the value object ForumState:</p>
                <p>&nbsp;</p>
                <table cellspacing="0" cellpadding="0">
                  <TR id="sl_svn103_21">
                    <TD>public interface ForumStateFactory { </TD>
                  </TR>
                  <TR id="sl_svn103_22">
                    <TD></TD>
                  </TR>
                  <TR id="sl_svn103_23">
                    <TD>        void init(Forum forum, ForumMessage lastPost); </TD>
                  </TR>
                  <TR id="sl_svn103_24">
                    <TD></TD>
                  </TR>
                  <TR id="sl_svn103_25">
                    <TD>        void addNewMessage(Forum forum, ForumMessage newLastPost); </TD>
                  </TR>
                  <TR id="sl_svn103_26">
                    <TD></TD>
                  </TR>
                  <TR id="sl_svn103_27">
                    <TD>        void addNewThread(Forum forum, ForumMessage newLastPost); </TD>
                  </TR>
                  <TR id="sl_svn103_28">
                    <TD></TD>
                  </TR>
                  <TR id="sl_svn103_29">
                    <TD>        void updateMessage(Forum forum, ForumMessage forumMessage); </TD>
                  </TR>
                  <TR id="sl_svn103_30">
                    <TD></TD>
                  </TR>
                  <TR id="sl_svn103_31">
                    <TD>}</TD>
                  </TR>
                </table>
                <p>&nbsp;</p>
                <p>　　what is difference this ForumStateFactory with other factory ? no synchronized method,there is a contrast as below:</p>
                <p>&nbsp;</p>
                <p><img src="images/DDD.png" alt="" width="813" height="378"><br>
                  Immutability always be there. we can find it by Value Object at first. and it   can improve our system performance.<BR>
                </p> 
                  <p>　　</p>
                  <p>&nbsp;</p>
                  <!-- #EndEditable -->
                 
			  	
          </td>
		  <td width="340" valign="top"  bgcolor="#FFFFD7"> 
          <div class="adsensf" id="vgad336x280">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 页上左336 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-7573657117119544"
     data-ad-slot="6751585519"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 页上左336 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-7573657117119544"
     data-ad-slot="6751585519"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>         
 </td>
  </tr>
</table>


<table width="100%" border="0" cellspacing="0" cellpadding="0" height="2" bgcolor="#000000"  align="center">
              <tr>
                <td></td>
              </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td height="20" bgcolor="#CAC9BB" align="center">
  
  </td>
</tr>
</table>
 </div>
</div><!-- /.container -->

<!-- InstanceBeginEditable name="EditRegion3" --><!-- InstanceEndEditable -->
        <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
		<script src="js/bootstrap.min.js"></script>
<!-- AddThis Smart Layers BEGIN -->
<!-- Go to http://www.addthis.com/get/smart-layers to customize -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52c4b79515624135"></script>
<script type="text/javascript">
  addthis.layers({
    'theme' : 'transparent',
    'share' : {
      'position' : 'left',
      'numPreferredServices' : 5
    },  
    'whatsnext' : {},  
    'recommended' : {} 
  });
</script>
<!-- AddThis Smart Layers END -->    
</body>
<!-- InstanceEnd --></html>
